//
//  MallGoodsCell.swift
//  Box
//
//  Created by mn on 2025/4/2.
//

import UIKit
import SnapKit

class MallGoodsCell: UICollectionViewCell {
    
    private let imageView = UIImageView()
    private let goldImageView = UIImageView(image: UIImage.gold)

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = .black
        label.numberOfLines = 2
        label.textAlignment = .left
        return label
    }()
        
    
    private let goldLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = .gold
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 8
        contentView.layer.masksToBounds = true
        contentView.addSubview(imageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(goldLabel)
        contentView.addSubview(goldImageView)

        
        imageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(imageView.snp.width).multipliedBy(0.75)
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(5)
            make.left.right.equalToSuperview().inset(10)
        }
        
        goldImageView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.left.equalToSuperview().inset(10)
        }
        goldLabel.snp.makeConstraints { make in
            make.left.equalTo(goldImageView.snp.right).offset(4)
            make.centerY.equalTo(goldImageView.snp.centerY)
        }
    }
    var goodsModel: GoodsModel?{
        didSet{
            guard let goodsModel else { return }
            titleLabel.text = goodsModel.name
            imageView.kf.setImage(with: URL(string: goodsModel.imgUrlList.first ?? ""))
            goldLabel.text = String(format: "%.2f", Float(goodsModel.price) * 0.01)
        }
    }
    
}
